#!/bin/bash

set -o errexit
#set -o pipefail

export LC_ALL=C

AN_DIR=$1

6_annotation/clear_bad_ann_dirs.py $AN_DIR

  i=0

  for fname in `join -t$'\t' -v1 \
      <(join -t$'\t' -1 2 -2 1 -v1 \
        <(find $AN_DIR/src -type f | xargs -n1 -I {} bash -c "echo -n -e {}'\t'; cat {}  | cut -f1" | sort -t$'\t' -k2,2) \
        <(find $AN_DIR/src_faa -type f | xargs -n1 -I {} bash -c "cat {}  | head -n1 | cut -b2-" | sort) \
      ) \
      <(find $AN_DIR/ -type d -name 'ann*' | sort | xargs -n1 -I {} bash -c "cat {}/gene.txt | cut -f1" | sort -u) \
      | cut -f2`; do

  fn=$(basename $fname)
  dn=$(dirname $fname)

  cat $AN_DIR/src/${fn} | awk -F'\t' '{print ">"$1"\n"$2}' > $AN_DIR/src_faa/${fn}.faa

  i=$(( i + 1 ))
  done

  echo "recovered $i files of ${AN_DIR}" >> /dev/stderr

